Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
96.55% |
84 / 87 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
| ServiceProvider | |
96.55% |
84 / 87 |
|
75.00% |
3 / 4 |
56 | |
0.00% |
0 / 1 |
| boot | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
6 | |||
| register | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
| about | |
95.24% |
60 / 63 |
|
0.00% |
0 / 1 |
48 | |||
| version | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | /** |
| 5 | * Playground |
| 6 | */ |
| 7 | namespace Playground\Auth; |
| 8 | |
| 9 | use Illuminate\Foundation\Console\AboutCommand; |
| 10 | use Illuminate\Foundation\Support\Providers\AuthServiceProvider; |
| 11 | |
| 12 | /** |
| 13 | * \Playground\Auth\ServiceProvider |
| 14 | */ |
| 15 | class ServiceProvider extends AuthServiceProvider |
| 16 | { |
| 17 | public const VERSION = '73.0.0'; |
| 18 | |
| 19 | protected string $package = 'playground-auth'; |
| 20 | |
| 21 | /** |
| 22 | * Bootstrap any package services. |
| 23 | * Register any authentication / authorization services. |
| 24 | * |
| 25 | * @return void |
| 26 | */ |
| 27 | public function boot() |
| 28 | { |
| 29 | /** |
| 30 | * @var array<string, mixed> $config |
| 31 | */ |
| 32 | $config = config($this->package); |
| 33 | |
| 34 | if (! empty($config['load']) && is_array($config['load'])) { |
| 35 | |
| 36 | if (! empty($config['load']['translations'])) { |
| 37 | $this->loadTranslationsFrom( |
| 38 | dirname(__DIR__).'/lang', |
| 39 | $this->package |
| 40 | ); |
| 41 | } |
| 42 | |
| 43 | if ($this->app->runningInConsole()) { |
| 44 | if (! empty($config['load']['commands'])) { |
| 45 | $this->commands([ |
| 46 | Console\Commands\HashPassword::class, |
| 47 | ]); |
| 48 | } |
| 49 | |
| 50 | // Publish configuration |
| 51 | $this->publishes([ |
| 52 | sprintf('%1$s/config/%2$s.php', dirname(__DIR__), $this->package) => config_path(sprintf('%1$s.php', $this->package)), |
| 53 | ], 'playground-config'); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | $this->about(); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Register any application services. |
| 62 | */ |
| 63 | public function register(): void |
| 64 | { |
| 65 | $this->mergeConfigFrom( |
| 66 | sprintf('%1$s/config/%2$s.php', dirname(__DIR__), $this->package), |
| 67 | $this->package |
| 68 | ); |
| 69 | |
| 70 | $this->app->scoped('playground-auth-can', function () { |
| 71 | return new Can(); |
| 72 | }); |
| 73 | } |
| 74 | |
| 75 | public function about(): void |
| 76 | { |
| 77 | $config = config($this->package); |
| 78 | $config = is_array($config) ? $config : []; |
| 79 | |
| 80 | $load = ! empty($config['load']) && is_array($config['load']) ? $config['load'] : []; |
| 81 | $token = ! empty($config['token']) && is_array($config['token']) ? $config['token'] : []; |
| 82 | |
| 83 | $listed_admins = 0; |
| 84 | if (! empty($config['admins']) && is_array($config['admins'])) { |
| 85 | $listed_admins = count($config['admins']); |
| 86 | } |
| 87 | |
| 88 | $listed_managers = 0; |
| 89 | if (! empty($config['managers']) && is_array($config['managers'])) { |
| 90 | $listed_managers = count($config['managers']); |
| 91 | } |
| 92 | |
| 93 | $abilties_root = 0; |
| 94 | $abilties_root_one = ''; |
| 95 | $abilties_admin = 0; |
| 96 | $abilties_admin_one = ''; |
| 97 | $abilties_manager = 0; |
| 98 | $abilties_manager_one = ''; |
| 99 | $abilties_user = 0; |
| 100 | $abilties_user_one = ''; |
| 101 | $abilties_guest = 0; |
| 102 | $abilties_guest_one = ''; |
| 103 | |
| 104 | if (! empty($config['abilities']) && is_array($config['abilities'])) { |
| 105 | // Check abilities: root |
| 106 | if (! empty($config['abilities']['root']) && is_array($config['abilities']['root'])) { |
| 107 | $abilties_root = count($config['abilities']['root']); |
| 108 | if ($abilties_root === 1) { |
| 109 | $abilties_root_one = json_encode($config['abilities']['root']); |
| 110 | } |
| 111 | } |
| 112 | // Check abilities: admin |
| 113 | if (! empty($config['abilities']['admin']) && is_array($config['abilities']['admin'])) { |
| 114 | $abilties_admin = count($config['abilities']['admin']); |
| 115 | if ($abilties_admin === 1) { |
| 116 | $abilties_admin_one = json_encode($config['abilities']['admin']); |
| 117 | } |
| 118 | } |
| 119 | // Check abilities: manager |
| 120 | if (! empty($config['abilities']['manager']) && is_array($config['abilities']['manager'])) { |
| 121 | $abilties_manager = count($config['abilities']['manager']); |
| 122 | if ($abilties_manager === 1) { |
| 123 | $abilties_manager_one = json_encode($config['abilities']['manager']); |
| 124 | } |
| 125 | } |
| 126 | // Check abilities: user |
| 127 | if (! empty($config['abilities']['user']) && is_array($config['abilities']['user'])) { |
| 128 | $abilties_user = count($config['abilities']['user']); |
| 129 | if ($abilties_user === 1) { |
| 130 | $abilties_user_one = json_encode($config['abilities']['user']); |
| 131 | } |
| 132 | } |
| 133 | // Check abilities: guest |
| 134 | if (! empty($config['abilities']['guest']) && is_array($config['abilities']['guest'])) { |
| 135 | $abilties_guest = count($config['abilities']['guest']); |
| 136 | if ($abilties_guest === 1) { |
| 137 | $abilties_guest_one = json_encode($config['abilities']['guest']); |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | $version = $this->version(); |
| 143 | |
| 144 | AboutCommand::add('Playground: Auth', fn () => [ |
| 145 | '<fg=yellow;options=bold>Load</> Commands' => ! empty($load['commands']) ? '<fg=green;options=bold>ENABLED</>' : '<fg=yellow;options=bold>DISABLED</>', |
| 146 | '<fg=yellow;options=bold>Load</> Translations' => ! empty($load['translations']) ? '<fg=green;options=bold>ENABLED</>' : '<fg=yellow;options=bold>DISABLED</>', |
| 147 | |
| 148 | '<fg=yellow;options=bold>Debug Mode</>' => ! empty($config['debug']) && ! empty(config('app.debug')) ? '<fg=yellow;options=bold>ENABLED</>' : '<fg=green;options=bold>OFF</>', |
| 149 | |
| 150 | '<fg=cyan;options=bold>Verify</>' => ! empty($config['verify']) && is_string($config['verify']) ? sprintf('[%s]', $config['verify']) : '', |
| 151 | |
| 152 | '<fg=cyan;options=bold>Token</> [Abilities]' => sprintf('[%s]', $token['abilities']), |
| 153 | '<fg=cyan;options=bold>Token</> [Expires]' => sprintf('[%s]', $token['expires']), |
| 154 | '<fg=cyan;options=bold>Token</> [Name]' => sprintf('[%s]', $token['name']), |
| 155 | '<fg=cyan;options=bold>Token Listed Admins</>' => ! empty($token['listed']) ? sprintf('<fg=green;options=bold>%1$d</>', $listed_admins) : '<fg=yellow;options=bold>DISABLED</>', |
| 156 | '<fg=cyan;options=bold>Token Listed Managers</>' => ! empty($token['listed']) ? sprintf('<fg=green;options=bold>%1$d</>', $listed_managers) : '<fg=yellow;options=bold>DISABLED</>', |
| 157 | '<fg=cyan;options=bold>Token Roles</>' => ! empty($token['roles']) ? '<fg=green;options=bold>ENABLED</>' : '<fg=yellow;options=bold>DISABLED</>', |
| 158 | '<fg=cyan;options=bold>Token Privileges</>' => ! empty($token['privileges']) ? '<fg=green;options=bold>ENABLED</>' : '<fg=yellow;options=bold>DISABLED</>', |
| 159 | '<fg=cyan;options=bold>Token Sanctum</>' => ! empty($token['sanctum']) ? '<fg=green;options=bold>ENABLED</>' : '<fg=yellow;options=bold>DISABLED</>', |
| 160 | |
| 161 | '<fg=cyan;options=bold>Abilities</> [admin]' => ! empty($abilties_admin) && empty($abilties_admin_one) ? sprintf('<fg=green;options=bold>%1$d</>', $abilties_admin) : sprintf('<fg=yellow;options=bold>%1$s</>', $abilties_admin_one), |
| 162 | '<fg=cyan;options=bold>Abilities</> [guest]' => ! empty($abilties_guest) && empty($abilties_guest_one) ? sprintf('<fg=green;options=bold>%1$d</>', $abilties_guest) : sprintf('<fg=yellow;options=bold>%1$s</>', $abilties_guest_one), |
| 163 | '<fg=cyan;options=bold>Abilities</> [manager]' => ! empty($abilties_manager) && empty($abilties_manager_one) ? sprintf('<fg=green;options=bold>%1$d</>', $abilties_manager) : sprintf('<fg=yellow;options=bold>%1$s</>', $abilties_manager_one), |
| 164 | '<fg=cyan;options=bold>Abilities</> [root]' => ! empty($abilties_root) && empty($abilties_root_one) ? sprintf('<fg=green;options=bold>%1$d</>', $abilties_root) : sprintf('<fg=yellow;options=bold>%1$s</>', $abilties_root_one), |
| 165 | '<fg=cyan;options=bold>Abilities</> [user]' => ! empty($abilties_user) && empty($abilties_user_one) ? sprintf('<fg=green;options=bold>%1$d</>', $abilties_user) : sprintf('<fg=yellow;options=bold>%1$s</>', $abilties_user_one), |
| 166 | |
| 167 | 'Package' => $this->package, |
| 168 | 'Version' => $version, |
| 169 | ]); |
| 170 | } |
| 171 | |
| 172 | public function version(): string |
| 173 | { |
| 174 | return static::VERSION; |
| 175 | } |
| 176 | } |